package com.test.jdbc.manager;

import com.test.jdbc.driver.Driver;
import com.test.jdbc.connection.RgsConnection;

import java.util.ArrayList;
import java.util.List;

/**
 * @author RenGaoshuai
 * @date 2024/6/3 10:59
 * @description 驱动的管理
 */
public class DriverManager {


    private static List<Driver> drivers = new ArrayList<Driver>();


    /**
     * 所有实现Driver接口的实现类都注册进来
     * @param driver
     */
    public static void register(Driver driver) {
        if (!drivers.contains(driver)) {
            drivers.add(driver);
        }
    }


    /**
     * 获取链接，频闭内部实现
     * @return
     */
    public static RgsConnection getConnection(String url){

        for (Driver driver : drivers) {
            RgsConnection connection = driver.getConnection(url);
            if (connection != null) {
                return connection;
            }
        }

        throw new RuntimeException("can not get connection");
    }
}
